package io.modifier.basic.compose.widget.weui.core.ui.components.button

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import io.modifier.basic.compose.theme.AppTheme
import io.modifier.basic.compose.theme.AppThemeHolder
import io.modifier.basic.compose.widget.weui.core.ui.components.loading.WeLoading
import io.modifier.basic.emo.core.runIf
import io.modifier.basic.emo.ui.core.modifier.throttleClick

//<editor-fold desc="preview">

@Preview
@Composable
private fun Preview() {
    AppTheme  {
        Column(
            Modifier
                .fillMaxSize()
                .background(AppThemeHolder.colors.appColor.copy(alpha = 0.05f)),
            verticalArrangement = Arrangement.spacedBy(15.dp)
        ) {

            Row {
                WeButton(text = "次要操作", type = ButtonType.PLAIN, size = ButtonSize.SMALL)

                Spacer(Modifier)

                WeButton(text = "警示操作", type = ButtonType.DANGER, size = ButtonSize.SMALL)
            }

            WeButton(text = "主要操作")
            WeButton(text = "主要操作", loading = true)
            WeButton(text = "次要操作", type = ButtonType.PLAIN)
            WeButton(text = "警示操作", type = ButtonType.DANGER)
            WeButton(text = "纯文字", type = ButtonType.TEXT, loading = true, disabled = true)
            Spacer(Modifier)
            WeButton(text = "按钮禁用", disabled = true)
            Spacer(Modifier)
            Row {
                WeButton(text = "small 按钮", size = ButtonSize.SMALL)
                Spacer(Modifier.width(12.dp))
                WeButton(text = "small 按钮", size = ButtonSize.SMALL, type = ButtonType.PLAIN)
                Spacer(Modifier.width(12.dp))
            }
        }
    }
}

//</editor-fold>

enum class ButtonType {
    PRIMARY,
    DANGER,
    PLAIN,
    PLAIN_DANGER,
    TEXT
}

enum class ButtonSize(
    val padding: PaddingValues,
    val fontSize: TextUnit,
    val borderRadius: Dp = 12.dp
) {
    LARGE(PaddingValues(horizontal = 36.dp), 16.sp),
    MEDIUM(PaddingValues(horizontal = 24.dp), 16.sp, 8.dp),
    SMALL(PaddingValues(horizontal = 12.dp), 14.sp, 6.dp),
}

/**
 * 按钮
 *
 * @param text 按钮文字
 * @param type 类型
 * @param size 大小
 * @param width 宽度
 * @param disabled 是否禁用
 * @param loading 是否加载中
 * @param onClick 点击事件
 */
@Composable
fun WeButton(
    text: String,
    modifier: Modifier = Modifier,
    type: ButtonType = ButtonType.PRIMARY,
    size: ButtonSize = if (type == ButtonType.TEXT) ButtonSize.SMALL else ButtonSize.LARGE,
    padding: PaddingValues = size.padding,
    fontSize: TextUnit = size.fontSize,
    borderRadius: Dp = size.borderRadius,
    buttonColors: ButtonColors? = null,
    width: Dp = 130.dp,
    height: Dp = 42.dp,
    disabled: Boolean = false,
    loading: Boolean = false,
    onClick: (() -> Unit)? = null
) {
    val colors = buttonColors ?: buttonColorSchemeOf(type)
    val localDisabled = disabled || loading

    Box(
        modifier
            .padding(padding)
            .runIf(size == ButtonSize.SMALL) {
                if (type == ButtonType.TEXT) {
                    wrapContentWidth()
                } else {
                    width(width)
                }
            }
            .runIf(size == ButtonSize.SMALL) {
                if (type == ButtonType.TEXT) {
                    wrapContentHeight()
                } else {
                    height(height)
                }
            }
            .runIf(size != ButtonSize.SMALL) { fillMaxWidth() }
            .runIf(size != ButtonSize.SMALL) { height(52.dp) }
            .clip(RoundedCornerShape(borderRadius))
            .throttleClick(
                enabled = !localDisabled && onClick != null
            ) {
                if (!localDisabled) {
                    onClick?.invoke()
                }
            }
            .background(colors.containerColor)
            .alpha(if (disabled) 0.6f else 1f)
            .runIf(type == ButtonType.TEXT) {
                padding(horizontal = 8.dp, vertical = 3.dp)
            },
        contentAlignment = Alignment.Center
    ) {
        Row(verticalAlignment = Alignment.CenterVertically) {
            if (loading) {
                WeLoading(color = colors.contentColor)
                Spacer(Modifier.width(4.dp))
            }

            Text(
                text,
                color = colors.contentColor,
                fontSize = fontSize
            )
        }
    }
}

fun padding(horizontal: Dp, vertical: Dp) {

}

data class ButtonColors(
    val containerColor: Color,
    val contentColor: Color
)

@Composable
private fun buttonColorSchemeOf(type: ButtonType): ButtonColors {
    return when (type) {
        ButtonType.PRIMARY -> {
            ButtonColors(AppThemeHolder.colors.appColor, Color.White)
        }

        ButtonType.DANGER -> {
            ButtonColors(AppThemeHolder.colors.error, AppThemeHolder.colors.onError)
        }

        ButtonType.PLAIN -> {
            ButtonColors(AppThemeHolder.colors.grayContainer, AppThemeHolder.colors.onGrayContainer)
        }

        ButtonType.PLAIN_DANGER -> {
            ButtonColors(AppThemeHolder.colors.surface, AppThemeHolder.colors.error)
        }

        ButtonType.TEXT -> {
            ButtonColors(Color.Transparent, AppThemeHolder.colors.appColor)
        }
    }
}